package cn.tedu.ams.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.tedu.ams.dao.ArticleDao;
import cn.tedu.ams.entity.Article;
import cn.tedu.ams.entity.User;

@WebServlet("/articleFindByPage.do")
public class ArticleFindByPage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null) {
			response.sendRedirect("login.jsp");
			return;
		}
		int page = 1;
		String strPage = request.getParameter("page");
		if(strPage != null) {
			page = Integer.parseInt(strPage);
		}
		
		ArticleDao dao = new ArticleDao();
		try {
			List<Article> list = dao.findByPage(page, 3);
			int maxPage = dao.maxPage(3);
			
			request.setAttribute("list", list);
			request.setAttribute("page", page);
			request.setAttribute("maxPage", maxPage);
			request.getRequestDispatcher("articleList.jsp").forward(request, response);;
		} catch (SQLException e) {
			e.printStackTrace();
			response.sendRedirect("error.jsp");
		}
	}

}
